package com.amoyhong.hongWhether.views;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;

import com.amoyhong.hongWhether.model.WeatherData;
import com.amoyhong.hongWhether.R;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2016/2/21.
 */
public class WeatherChart {

    private Context context;
    private List<WeatherData> temperatureValues;
    private Double maxY, minY;
    private static float THREE_HOUR = 3 * 60 * 1000.0f;
    private int screenDensity;

    public WeatherChart(Context context, List<WeatherData> temperatureValues, int screenDensity) {
        this.context = context;
        this.temperatureValues = temperatureValues;
        this.screenDensity = screenDensity;
    }

    public View getChart() {
        XYMultipleSeriesDataset xyMultipleSeriesDataset = generateDataset();
        XYMultipleSeriesRenderer xyMultipleSeriesRenderer = generateRender();
        return ChartFactory.getTimeChartView(context, xyMultipleSeriesDataset, xyMultipleSeriesRenderer, "M-dd HH:mm");
    }

    private XYMultipleSeriesRenderer generateRender() {
        XYMultipleSeriesRenderer multipleSeriesRenderer = new XYMultipleSeriesRenderer();

        XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
        seriesRenderer.setColor(context.getResources().getColor(R.color.colorPrimaryDark));
        seriesRenderer.setPointStyle(PointStyle.CIRCLE);
        seriesRenderer.setFillPoints(true);
        seriesRenderer.setDisplayChartValues(true);
        seriesRenderer.setLineWidth(getPixelByDensity(8));
        seriesRenderer.setChartValuesTextSize(getPixelByDensity(35));
        seriesRenderer.setChartValuesFormat(new DecimalFormat(".#"));
        seriesRenderer.setChartValuesTextAlign(Paint.Align.CENTER);
        seriesRenderer.setChartValuesSpacing(getPixelByDensity(50));
        seriesRenderer.setDisplayChartValuesDistance((int) getPixelByDensity(10));
        multipleSeriesRenderer.addSeriesRenderer(seriesRenderer);

        multipleSeriesRenderer.setXAxisMax(temperatureValues.get(temperatureValues.size() / 3).getDate().getTime() + 0.0f);
        multipleSeriesRenderer.setXAxisMin(temperatureValues.get(0).getDate().getTime() - THREE_HOUR);
//        multipleSeriesRenderer.setXLabels(temperatureValues.size()/2);


        multipleSeriesRenderer.setYAxisMax(maxY + 5);
        multipleSeriesRenderer.setYTitle("温度");
        multipleSeriesRenderer.setAxisTitleTextSize(getPixelByDensity(50));
        multipleSeriesRenderer.setYAxisMin(minY - 3);
        multipleSeriesRenderer.setYLabels(10);
        multipleSeriesRenderer.setPointSize(getPixelByDensity(16.0f));
        multipleSeriesRenderer.setLabelsTextSize(getPixelByDensity(50));
        multipleSeriesRenderer.setYLabelsPadding(getPixelByDensity(10));
        multipleSeriesRenderer.setYAxisAlign(Paint.Align.LEFT, 0);
        multipleSeriesRenderer.setYLabelsAlign(Paint.Align.RIGHT);

        multipleSeriesRenderer.setZoomEnabled(true, false);
        multipleSeriesRenderer.setPanEnabled(true, false);
//        multipleSeriesRenderer.setPanLimits(new double[]{0.0f, temperatureValues.size(), minY - 5, maxY + 5});


        multipleSeriesRenderer.setAxesColor(context.getResources().getColor(R.color.chart_axies_color));
        multipleSeriesRenderer.setLabelsColor(context.getResources().getColor(R.color.chart_labels_color));

        multipleSeriesRenderer.setMargins(new int[]{(int) getPixelByDensity(50), (int) getPixelByDensity(100), (int) getPixelByDensity(20), (int) getPixelByDensity(50)});
        multipleSeriesRenderer.setShowLegend(false);

        int bgColor = context.getResources().getColor(R.color.page_chart_bg_color);
        multipleSeriesRenderer.setBackgroundColor(bgColor);
        multipleSeriesRenderer.setMarginsColor(bgColor);

        return multipleSeriesRenderer;
    }

    private float getPixelByDensity(float value) {
        return value / 560 * screenDensity;
    }

    private XYMultipleSeriesDataset generateDataset() {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        TimeSeries xySeries = new TimeSeries("Temperature");

        int size = temperatureValues.size();
        if (size == 0) {
            xySeries.add(0, 0);
        } else {
            minY = maxY = temperatureValues.get(0).getTemperature();
        }

        for (int i = 0; i < size; i++) {
            WeatherData weatherData = temperatureValues.get(i);
            Double temp = weatherData.getTemperature();
            xySeries.add(weatherData.getDate(), temp);

            if (temp > maxY) {
                maxY = temp;
            }

            if (temp < minY) {
                minY = temp;
            }
        }
        dataset.addSeries(xySeries);
        return dataset;
    }
}
